package com.itlong.cloud.controller.operate;

import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.POJO.VO.operate.OperateAddressVO;
import com.itlong.cloud.POJO.VO.operate.OperateAreaAddressVO;
import com.itlong.cloud.POJO.VO.operate.OperateCountryAddressVO;
import com.itlong.cloud.abstracts.BaseController;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.enumerate.ecode.OperateErrorCodeEnum;
import com.itlong.cloud.equip.operate.IOperateAddressManageServiceClient;
import com.itlong.cloud.thrown.DataAccessException;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <desc>
 *     运营—地址管理（国家和区域信息）。
 * <desc/>
 *
 * @createDate 2017/10/23.
 */
@RestController
@RequestMapping("/addressManage")
public class OperateAddressManageController extends BaseController {

    private static final Logger LOG = LoggerFactory.getLogger(OperateAddressManageController.class);

    //地址管理
    @Autowired
    IOperateAddressManageServiceClient iOperateAddressManageServiceClient;

     /**
      * <desc>
      *      查询国家列表信息。
      * <desc/>
      *
      * @param
      * @return 返回国家列表信息（国家ID 和 国家名称）
      * @author shengen.T
      * @createDate 2017/10/23
      */
    @RequestMapping(path = "/getCountry" , method = RequestMethod.POST)
    @ResponseBody
    public Object getCountry(){
       MessageVO messageVO;
       try {
           List<OperateCountryAddressVO> countryList = iOperateAddressManageServiceClient.getCountry();
           messageVO = new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),countryList);
       }catch (Exception e){
           throw new DataAccessException("【运营】获取国家列表信息失败",e);
       }
       return messageVO;
    }

     /**
      * <desc>
      *      获取区域列表。
      * <desc/>
      *
      * @param  countryId 国家ID
      * @return 返回区域列表信息 （区域ID 和 区域名称）
      * @author shengen.T
      * @createDate 2017/10/23
      */
    @RequestMapping(path = "/getArea" , method = RequestMethod.POST)
    @ResponseBody
    public Object getArea(String countryId){
        MessageVO messageVO;
        try{
            HttpServletRequest request= ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            countryId=request.getAttribute("countryId")==null?null:request.getAttribute("countryId").toString();
            if(StringUtils.isEmpty(countryId)){
               return new MessageVO(OperateErrorCodeEnum.COUNTRY_ID_EMPTY_ERR.getErrorCode()) ;
            }
            List<OperateAreaAddressVO> areaList = iOperateAddressManageServiceClient.getArea(countryId);
            messageVO = new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),areaList);
        }catch (Exception e){
            throw new DataAccessException("【运营】获取区域列表信息失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      获取地址信息列表
     * <desc/>
     *
     * @param
     * @return
     * @author Chunjian.G
     * @createDate 2017-12-05
     */
    @RequestMapping(path = "/getAddress" , method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Object getAddress(String parentAddressId){
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            parentAddressId= request.getAttribute("parentAddressId")==null?null:request.getAttribute("parentAddressId").toString();
            if (StringUtils.isBlank(parentAddressId)){
                parentAddressId = "0";
            }
            List<OperateAddressVO> addrs = iOperateAddressManageServiceClient.getAddress(parentAddressId);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), addrs);
        }catch (Exception e){
            throw new DataAccessException("【运营】获取地址信息列表失败",e);
        }
    }
}
